package com.cxd.app.entity;

// default package

import java.util.Date;
import java.util.List;

import com.cxd.app.entity.base.LoanRepayBase;
import com.cxd.base.common.SpringContextUtil;
import com.cxd.base.common.constants.LoanConstants.RepayStatus;
import com.cxd.base.util.ArithUtil;
import com.cxd.base.util.BeanTools;

/**
 * Loan entity. // TODO:竞标借款， 担保借款之类
 */
public class Loan{
	private String loanId;
	@SuppressWarnings("unused")
	private Loan(){
		
	}
	public Loan(String loanId){
		this.loanId = loanId;
	}
	/**
	 * 还款路标（待还、已还金额之类）
	 */
	private RepayRoadmap repayRoadmap;
	public RepayRoadmap getRepayRoadmap() {
		if (this.repayRoadmap == null) {
			// 未还本金
			Double unPaidCorpus = 0D;
			// 未还利息
			Double unPaidInterest = 0D;
			// 未还手续费
			Double unPaidFee = 0D;
			// 未还罚息
			Double unPaidDefaultInterest = 0D;
			// 已还本金
			Double paidCorpus = 0D;
			// 已还利息
			Double paidInterest = 0D;
			// 已还手续费
			Double paidFee = 0D;
			// 已还罚息
			Double paidDefaultInterest = 0D;
			// 下个还款日
			Date nextRepayDate = null;
			// 下个还款本金
			Double nextRepayCorpus = 0d;
			// 下个还款利息
			Double nextRepayInterest = 0d;
			// 下个还款手续费
			Double nextRepayFee = 0d;
			// 下个还款罚息
			Double nextRepayDefaultInterest = 0d;
			// 已还期数
			int paidPeriod = 0;
			int loanRepaysSize = 0;
			List<LoanRepayBase> listLoanRepayList = getLoanRepays();
			if(listLoanRepayList!=null){
				loanRepaysSize = getLoanRepays().size();
				for (int i = 0; i < loanRepaysSize; i++) {
					LoanRepayBase ir = getLoanRepays().get(i);
					if (ir.getStatus().equals(RepayStatus.BAD_DEBT)
							|| ir.getStatus().equals(RepayStatus.OVERDUE)
							|| ir.getStatus().equals(RepayStatus.REPAYING)) {
						unPaidCorpus = ArithUtil.add(unPaidCorpus, ir.getCorpus());
						unPaidInterest = ArithUtil.add(unPaidInterest,
								ir.getInterest());
						unPaidFee = ArithUtil.add(unPaidFee, ir.getFee());
						unPaidDefaultInterest = ArithUtil.add(
								unPaidDefaultInterest, ir.getDefaultInterest());
						if (i == 0
								|| getLoanRepays().get(i - 1).getStatus()
										.equals(RepayStatus.COMPLETE)) {
							// 下一期待还款
							nextRepayDate = ir.getRepayDay();
							nextRepayCorpus = ir.getCorpus();
							nextRepayInterest = ir.getInterest();
							nextRepayFee = ir.getFee();
							nextRepayDefaultInterest = ir.getDefaultInterest();
						}
					} else if (ir.getStatus().equals(RepayStatus.COMPLETE)) {
						paidCorpus = ArithUtil.add(paidCorpus, ir.getCorpus());
						paidInterest = ArithUtil
								.add(paidInterest, ir.getInterest());
						paidFee = ArithUtil.add(paidFee, ir.getFee());
						paidDefaultInterest = ArithUtil.add(paidDefaultInterest,
								ir.getDefaultInterest());
						paidPeriod++;
					}
				}
			}

			this.repayRoadmap = new RepayRoadmap(unPaidCorpus, unPaidInterest,
					unPaidFee, unPaidDefaultInterest, paidCorpus, paidInterest,
					paidFee, paidDefaultInterest, nextRepayDate,
					nextRepayCorpus, nextRepayInterest, nextRepayFee,
					nextRepayDefaultInterest, paidPeriod, loanRepaysSize - paidPeriod, true);
		}
		return this.repayRoadmap;
	}
	
	public List<LoanRepayBase> getLoanRepays(){
		BeanTools db = (BeanTools)SpringContextUtil.getBean("db");
		List<LoanRepayBase> listBean = db.queryListBean("LOAN_REPAY", "LOAN_ID", loanId, LoanRepayBase.class);
		return listBean;
	}
}
